button.js ➔ Button   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
/*!
2
  * Bootstrap button.js v4.6.2 (https://getbootstrap.com/)
3
  * Copyright 2011-2022 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
8
  typeof define === 'function' && define.amd ? define(['jquery'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.jQuery));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
})(this, (function ($) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
16
  function _defineProperties(target, props) {
17
    for (var i = 0; i < props.length; i++) {
18
      var descriptor = props[i];
19
      descriptor.enumerable = descriptor.enumerable || false;
20
      descriptor.configurable = true;
21
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
      Object.defineProperty(target, descriptor.key, descriptor);
23
    }
24
  }
25
26
  function _createClass(Constructor, protoProps, staticProps) {
27
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    Object.defineProperty(Constructor, "prototype", {
30
      writable: false
31
    });
32
    return Constructor;
33
  }
34
35
  /**
36
   * Constants
37
   */
38
39
  var NAME = 'button';
40
  var VERSION = '4.6.2';
41
  var DATA_KEY = 'bs.button';
42
  var EVENT_KEY = "." + DATA_KEY;
43
  var DATA_API_KEY = '.data-api';
44
  var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
45
  var CLASS_NAME_ACTIVE = 'active';
46
  var CLASS_NAME_BUTTON = 'btn';
47
  var CLASS_NAME_FOCUS = 'focus';
48
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
49
  var EVENT_FOCUS_BLUR_DATA_API = "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY);
50
  var EVENT_LOAD_DATA_API = "load" + EVENT_KEY + DATA_API_KEY;
51
  var SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]';
52
  var SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]';
53
  var SELECTOR_DATA_TOGGLE = '[data-toggle="button"]';
54
  var SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn';
55
  var SELECTOR_INPUT = 'input:not([type="hidden"])';
56
  var SELECTOR_ACTIVE = '.active';
57
  var SELECTOR_BUTTON = '.btn';
58
  /**
59
   * Class definition
60
   */
61
62
  var Button = /*#__PURE__*/function () {
63
    function Button(element) {
64
      this._element = element;
65
      this.shouldAvoidTriggerChange = false;
66
    } // Getters
67
68
69
    var _proto = Button.prototype;
70
71
    // Public
72
    _proto.toggle = function toggle() {
73
      var triggerChangeEvent = true;
74
      var addAriaPressed = true;
75
      var rootElement = $__default["default"](this._element).closest(SELECTOR_DATA_TOGGLES)[0];
76
77
      if (rootElement) {
78
        var input = this._element.querySelector(SELECTOR_INPUT);
79
80
        if (input) {
81
          if (input.type === 'radio') {
82
            if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
83
              triggerChangeEvent = false;
84
            } else {
85
              var activeElement = rootElement.querySelector(SELECTOR_ACTIVE);
86
87
              if (activeElement) {
88
                $__default["default"](activeElement).removeClass(CLASS_NAME_ACTIVE);
89
              }
90
            }
91
          }
92
93
          if (triggerChangeEvent) {
94
            // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
95
            if (input.type === 'checkbox' || input.type === 'radio') {
96
              input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE);
97
            }
98
99
            if (!this.shouldAvoidTriggerChange) {
100
              $__default["default"](input).trigger('change');
101
            }
102
          }
103
104
          input.focus();
105
          addAriaPressed = false;
106
        }
107
      }
108
109
      if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
110
        if (addAriaPressed) {
111
          this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE));
112
        }
113
114
        if (triggerChangeEvent) {
115
          $__default["default"](this._element).toggleClass(CLASS_NAME_ACTIVE);
116
        }
117
      }
118
    };
119
120
    _proto.dispose = function dispose() {
121
      $__default["default"].removeData(this._element, DATA_KEY);
122
      this._element = null;
123
    } // Static
124
    ;
125
126
    Button._jQueryInterface = function _jQueryInterface(config, avoidTriggerChange) {
127
      return this.each(function () {
128
        var $element = $__default["default"](this);
129
        var data = $element.data(DATA_KEY);
130
131
        if (!data) {
132
          data = new Button(this);
133
          $element.data(DATA_KEY, data);
134
        }
135
136
        data.shouldAvoidTriggerChange = avoidTriggerChange;
137
138
        if (config === 'toggle') {
139
          data[config]();
140
        }
141
      });
142
    };
143
144
    _createClass(Button, null, [{
145
      key: "VERSION",
146
      get: function get() {
147
        return VERSION;
148
      }
149
    }]);
150
151
    return Button;
152
  }();
153
  /**
154
   * Data API implementation
155
   */
156
157
158
  $__default["default"](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
159
    var button = event.target;
160
    var initialButton = button;
161
162
    if (!$__default["default"](button).hasClass(CLASS_NAME_BUTTON)) {
163
      button = $__default["default"](button).closest(SELECTOR_BUTTON)[0];
164
    }
165
166
    if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
167
      event.preventDefault(); // work around Firefox bug #1540995
168
    } else {
169
      var inputBtn = button.querySelector(SELECTOR_INPUT);
170
171
      if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
172
        event.preventDefault(); // work around Firefox bug #1540995
173
174
        return;
175
      }
176
177
      if (initialButton.tagName === 'INPUT' || button.tagName !== 'LABEL') {
178
        Button._jQueryInterface.call($__default["default"](button), 'toggle', initialButton.tagName === 'INPUT');
179
      }
180
    }
181
  }).on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
182
    var button = $__default["default"](event.target).closest(SELECTOR_BUTTON)[0];
183
    $__default["default"](button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type));
184
  });
185
  $__default["default"](window).on(EVENT_LOAD_DATA_API, function () {
186
    // ensure correct active class is set to match the controls' actual values/states
187
    // find all checkboxes/readio buttons inside data-toggle groups
188
    var buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS));
189
190
    for (var i = 0, len = buttons.length; i < len; i++) {
191
      var button = buttons[i];
192
      var input = button.querySelector(SELECTOR_INPUT);
193
194
      if (input.checked || input.hasAttribute('checked')) {
195
        button.classList.add(CLASS_NAME_ACTIVE);
196
      } else {
197
        button.classList.remove(CLASS_NAME_ACTIVE);
198
      }
199
    } // find all button toggles
200
201
202
    buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
203
204
    for (var _i = 0, _len = buttons.length; _i < _len; _i++) {
205
      var _button = buttons[_i];
206
207
      if (_button.getAttribute('aria-pressed') === 'true') {
208
        _button.classList.add(CLASS_NAME_ACTIVE);
209
      } else {
210
        _button.classList.remove(CLASS_NAME_ACTIVE);
211
      }
212
    }
213
  });
214
  /**
215
   * jQuery
216
   */
217
218
  $__default["default"].fn[NAME] = Button._jQueryInterface;
219
  $__default["default"].fn[NAME].Constructor = Button;
220
221
  $__default["default"].fn[NAME].noConflict = function () {
222
    $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
223
    return Button._jQueryInterface;
224
  };
225
226
  return Button;
227
228
}));
229
//# sourceMappingURL=button.js.map
230